package com.dong.springboothikaricpmultiple;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;

@SpringBootApplication
public class SpringBootHikaricpMultipleApplication implements CommandLineRunner {


    private Logger logger = LoggerFactory.getLogger(SpringBootHikaricpMultipleApplication.class);

    @Resource(name = "ordersDataSource")
    DataSource ordersDataSource;

    @Resource(name = "usersDataSource")
    DataSource usersDataSource;

    public static void main(String[] args) {
        SpringApplication.run(SpringBootHikaricpMultipleApplication.class, args);
    }


    @Override
    public void run(String... args) throws Exception {
        // orders 数据源
        try (Connection conn = ordersDataSource.getConnection()) {
            // ...可以做其他操作
            logger.info("【run】【ordersDataSource获得连接，{}】", conn);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // users 数据源

        try (Connection conn = usersDataSource.getConnection()) {
            // ...可以做其他操作
            logger.info("【run】【usersDataSource获得连接，{}】", conn);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
